(function ($) {
	var pageType,source,perInfo ;
	var wardID = ''
	$.init();
	$.plusReady(function() {
		baseSetup();
	})
	if (!mui.os.plus) {
		baseSetup()
	}
	//护士id^层级^病区^职称^论文题目^ 论文类型^id^状态
	function submitNewData(){
		blurAllInput()
		//parr: 1^5^2022-06-10^00:00^2022-06-10^23:59^^^^^
		var postDom = $('#applyType .schItem.mui-active')[0]
		if (!postDom) {
			mui.toast('请选择请假类型')
			return
		}
		var startDate = $('#startDate')[0].innerText
		var endDate = $('#endDate')[0].innerText
		if (startDate.indexOf('日期') != -1) {
			mui.toast('请选择请假开始日期')
			return
		}
		if (endDate.indexOf('日期') != -1) {
			mui.toast('请选择请假结束日期')
			return
		}
		if (endDate < startDate) {
			mui.toast('结束日期不能早于开始日期')
			return
		}
		var parr = wardID+'^'+postDom.getAttribute('data-rw')+'^'+startDate+'^00:00^'+endDate+'^23:59'
		if (source == 'H') {
			//1^46^2022-06-08^00:00^2022-06-08^23:59^请假事由^所在地^负责人^13262156210^
			var reason = $('#reason')[0].value
			if (reason == '') {
				mui.toast('请填写请假事由')
				return
			}
			parr += '^'+reason
			var location = $('#location')[0].value
			if (location == '') {
				mui.toast('请填写请假所在地')
				return
			}
			parr += '^'+location
			/*var resPer = $('#resPer')[0].innerText
			if (resPer == '') {
				mui.toast('请选择临时负责人')
				return
			}*/
			var resPer = $('#resPer')[0].value
			if (resPer == '') {
				mui.toast('请填写临时负责人')
				return
			}
			parr += '^'+resPer
			var tel = $('#tel')[0].value
			if (tel == '') {
				mui.toast('请填写临时负责人电话')
				return
			}
			parr += '^'+tel+'^'
		}else{
			parr += '^^^^^';
		}
		plus.nativeUI.showWaiting("正在提交",{modal: false})
		var pdict = {'nurseid':perInfo['loginPerID'],'parr':parr,'source':source}
		var paramDict = {'method':'web.INMLeaveComm:SaveLeaveApp','params':JSON.stringify(pdict),'type':'Method'}
		$.DHCWebService(paramDict, function(result) {
			//console.log(JSON.stringify(result))
			plus.nativeUI.closeWaiting();
			if (result.msg != "1") {
				mui.toast(result.msg)
				return 
			}
			var listPage = plus.webview.getWebviewById('../WKPlatformPages/leave/pmLeaveApply.html')
			listPage.evalJS("detailChanged()")
			mui.back()
		}, function(errorStr) {
			mui.toast('请重试')
			plus.nativeUI.closeWaiting();
		});
	}
	
	//type P:排班 H:休假 S:特殊 意愿排班传P即
	function getPostData(){
		var applyType = $('#applyType')[0]
		var pdict = {"start":0,'limit':"",'parr':'^'+wardID+'^H'} //P:排班 H:休假 S:特殊
		var paramDict = {'method':'web.INMDBComm:FindWardPostList','params':JSON.stringify(pdict),'type':'RecQuery'}
		//console.log(JSON.stringify(pdict))
		$.DHCWebService(paramDict, function(result) {
			//console.log(JSON.stringify(result))
			if (result['msgcode'] != '1') {
				mui.toast(result['msg'])
				applyType.innerHTML = ''
				return
			}
			var dataArr = result['data']
			var html = ''
			for (var i = 0; i < dataArr.length; i++) {
				var oneData = dataArr[i]
				html +='<div class="schItem" data-index="'+i+'" data-rw="'+oneData['rw']+'">'+oneData['PostDesc']+'</div>'
			}
			applyType.innerHTML = html
			
		}, function(errorStr) {
			applyType.innerHTML = ''
		});
	}
	//选择日期
	function selectDate(){
		blurAllInput()
		var _self = this;
		plus.nativeUI.pickDate(function(e) {
			var d = e.date;
			var dateStr = d.getFullYear()+'-'
			if (d.getMonth() < 9) {
				dateStr += '0'
			}
			dateStr += (d.getMonth() + 1)+'-'
			if (d.getDate() < 10) {
				dateStr += '0'
			}
			dateStr += d.getDate()
			_self.innerText = dateStr;
			_self.classList.remove('noTextTip')
			computeDayLength();
		}, function(e) {}, {});
	}
	function computeDayLength(){
		var startDate = $('#startDate')[0].innerText
		var endDate = $('#endDate')[0].innerText
		if (startDate.indexOf('日期') != -1 || endDate.indexOf('日期') != -1) {
			return
		}
		var dateLength = $('#dateLength')[0]
		if (endDate < startDate) {
			dateLength.innerText = ''
			return
		}
		if (startDate == endDate) {
			dateLength.innerText = '1天'
		}
		var start = new Date(startDate).getTime();
		var end = new Date(endDate).getTime();
		var minsec = end - start
		var days = minsec / (24*60*60000) + 1
		dateLength.innerText = days +'天'
	}
	function blurAllInput(){
		mui('body input').each(function (index,element) {
			element.blur()
		})
	}
	
	function selectPostItem(){
		var oldActive = $('#applyType .schItem.mui-active')[0]
		if (oldActive && oldActive != this) {
			oldActive.classList.remove('mui-active')
		}
		this.classList.add('mui-active')
	}
	
	function baseSetup(){
		$('.mui-scroll-wrapper').scroll({
			bounce: true,
			indicators: false, //是否显示滚动条
		});
		var self = plus.webview.currentWebview()
		pageType = self.title
		perInfo = app.getState()
		wardID = perInfo.loginWardId
		switch (pageType){
			case '请假申请':
				source="N"
				break;
			case '护士长请假':
				source="H"
				mui('.hide').each(function (index,element) {
					element.classList.remove('hide')
				})
				break;
			default:
				break;
		}
		mui('#UL').on('tap','.schItem',selectPostItem)
		//提交按钮
		mui('body').on('tap','.submitBtn',submitNewData)
		// 日期
		mui('#UL').on('tap','.dateType',selectDate);
		//临时负责人
		/*mui('#UL').on('tap','#resPer',function(){
			var pageId = plus.webview.currentWebview().id
			var webview = plus.webview.create('../kySelectPerson.html', 'kySelectPerson', {
				background: '#fff',
				statusbar: {
					background: '#fff'
				}
			},{'pageId':pageId,'domId':'resPer','type':'单选'});
			webview.show('fade-in');
		});*/
		getPostData()
	}
})(mui)

function selectPerson(domId,params){
	if (params.length == 0) {
		return
	}
	mui('#'+domId)[0].innerText = params[0]['perName']
	mui('#tel')[0].value = ''
}


/*

*/